# -*- coding: utf-8 -*-
# main.py

from Calculator.CalcualtorUI import Ui_MainWindow
import sys, os
from PyQt5.QtWidgets import (QMainWindow,QApplication,)


class MyMainWidgets(QMainWindow, Ui_MainWindow):
    da1=''
    da2=''
    flag=False
    opt=''
    def __init__(self, parent=None):
        super(MyMainWidgets, self).__init__(parent)
        self.setupUi(self)
        self.BtnEvent()

    def BtnEvent(self):
        self.Btn0.clicked.connect(self.Btn0Event)
        self.Btn1.clicked.connect(self.Btn1Event)
        self.Btn2.clicked.connect(self.Btn2Event)
        self.Btn3.clicked.connect(self.Btn3Event)
        self.Btn4.clicked.connect(self.Btn4Event)
        self.Btn5.clicked.connect(self.Btn5Event)
        self.Btn6.clicked.connect(self.Btn6Event)
        self.Btn7.clicked.connect(self.Btn7Event)
        self.Btn8.clicked.connect(self.Btn8Event)
        self.Btn9.clicked.connect(self.Btn9Event)
        self.BtnDot.clicked.connect(self.BtnDotEvent)
        self.BtnPlus.clicked.connect(self.BtnPlusEvent)
        self.BtnMinus.clicked.connect(self.BtnMinusEvent)
        self.BtnBy.clicked.connect(self.BtnByEvent)
        self.BtnDivid.clicked.connect(self.BtnDividEvent)
        self.BtnEqual.clicked.connect(self.BtnEqualEvent)
        self.BtnClear.clicked.connect(self.BtnClearEvent)
    def SetNum(self,val):
        if self.flag==False:
            self.da1=self.da1+str(val)
            self.ShowRes.display(self.da1)
        else:
            self.da2=self.da2+str(val)
            self.ShowRes.display(self.da2)

    def Btn1Event(self):
        self.SetNum(1)

    def Btn2Event(self):
        self.SetNum(2)

    def Btn3Event(self):
        self.SetNum(3)

    def Btn4Event(self):
        self.SetNum(4)

    def Btn5Event(self):
        self.SetNum(5)

    def Btn6Event(self):
        self.SetNum(6)

    def Btn7Event(self):
        self.SetNum(7)

    def Btn8Event(self):
        self.SetNum(8)

    def Btn9Event(self):
        self.SetNum(9)

    def Btn0Event(self):
        self.SetNum(0)

    def BtnDotEvent(self):
        self.SetNum('.')

    def BtnPlusEvent(self):
        self.opt='+'
        self.flag=True

    def BtnMinusEvent(self):
        self.opt='-'
        self.flag=True

    def BtnByEvent(self):
        self.opt='*'
        self.flag=True

    def BtnDividEvent(self):
        self.opt='/'
        self.flag=True

    def BtnEqualEvent(self):
        num1=float(self.da1)
        num2=float(self.da2)
        try:
            if self.opt == '+':
                res = num1 + num2
            elif self.opt == '-':
                res = num1 - num2
            elif self.opt == '*':
                res = num1 * num2
            elif self.opt == '/':
                if num2 != 0:
                    res = num1 / num2
                else:
                    res = 0
        except Exception as e:
            print(e)
        self.ShowRes.display(res)

    def BtnClearEvent(self):
        self.da1=''
        self.da2=''
        self.opt=''
        self.ShowRes.display('0')



if __name__ == '__main__':
    app = QApplication(sys.argv)
    myWin = MyMainWidgets()
    # myWin.setWindowFlags(QtCore.Qt.FramelessWindowHint)
    # styleFile =os.getcwd()+'\style.qss'
    # style = CommonHelper.readQss(styleFile)
    # myWin.setStyleSheet(style)
    myWin.show()
    sys.exit(app.exec_())